package com.zm.demo.core.leetcode;

import java.util.HashMap;
import java.util.Map;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2020/1/8 10:28
 */
public class IsHappy {

    public boolean isHappy(int n) {
        Map<Integer, Integer> history = new HashMap<>();
        return isHappy(n, history);
    }

    private boolean isHappy(int n,Map<Integer, Integer> history){
        int sum = 0;
        while (n/10 > 0){
            sum += (n%10) * (n%10);
            n= n/10;
        }
        sum += (n * n);
        if (sum == 1){
            return true;
        }else if (history.get(sum) != null){
            return false;
        }else {
            history.put(sum, 1);
            return isHappy(sum, history);
        }
    }



    public static void main(String[]args){
        out.println(new IsHappy().isHappy(11111111));
    }
}
